global restrdir "U:\data"
global public "R:\Public"
global rand "R:\Public\Contributions\Rand\RandHRS2016V2\stata"
global output "U:\output"

*------------------------------------------------------------------------------*
* Estimate non-linear wage betas to account foe age-specific changes in 
* wages in the pension wealth estimates
*------------------------------------------------------------------------------*

*Start with RAND 1992-2016 file

use hhid pn hhidpn ragender inw* r*iearn r*iwendy r*agey_b r*jcpen using $rand/randhrs1992_2016v2, clear

*Prepare varnames for reshaping

forvalues t = 1/12{
		rename r`t'iearn earn`t'
		rename r`t'agey_b age`t'
		rename r`t'iwendy iwyear`t'
		rename r`t'jcpen jcpen`t'
}

*Reshape the data
reshape long earn age iwyear inw jcpen, i(hhidpn) j(wave)

*Variables for regression
gen age2 = age*age
gen logearn = ln(earn)

*First wave for each R
bysort hhidpn: egen firstiw = min(iwyear)
*Years between waves
gen t = iwyear - firstiw
*Drop obs where respondent is not interviewed/no age available
drop if age==.

* Log earnings trajectory regression...
* t controls for annual nominal wage growth 
* jcpen=1 for respondents who have employer-sponsored pensions (DB or DC)
* age<48 is uncommon in the HRS; betas estimated off variation in age 48-80
//Male
reg logearn t age age2 if ragender==1 & jcpen==1 & inrange(age,48,80)
local nlwb1M = e(b)[1,2]
local nlwb2M = e(b)[1,3]
//Female
reg logearn t age age2 if ragender==2 & jcpen==1 & inrange(age,48,80)
local nlwb1F = e(b)[1,2]
local nlwb2F = e(b)[1,3]

*Input respondent file from PEP and do age adjustment (betas)
import delimited using $restrdir/Respondents10_orig.csv, clear
/*Estimates of age curvature from RAND 1992-2016 file*/
replace nonlinearwagebeta1 = `nlwb1M' if sex==1
replace nonlinearwagebeta2 = `nlwb2M' if sex==1
replace nonlinearwagebeta1 = `nlwb1F' if sex==2
replace nonlinearwagebeta2 = `nlwb2F' if sex==2
export delimited using $restrdir/Respondents10_wbetas.csv, replace
